﻿using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class MeasurementPanel : /*ViewBase*/ MonoBehaviour
{
    public Toggle btnstartmesaure;

    public Toggle xyzOpen;

    public Slider sliderSpeed;

    public Slider sliderScale;

    //private CameraCtrlogic cameraCtr;

    public DragItemUI drag;

    private Vector3 originalPos;

    public RectTransform content;

    public GameObject historyItem;

    private void Awake()
    {
        //cameraCtr = Camera.main.GetComponent<CameraCtrlogic>();

        originalPos = drag.transform.position;

        drag.gameObject.SetActive(false);
    }

    private void Start()
    {
        btnstartmesaure.onValueChanged.AddListener((ison) => 
        {
            if (ison)
            {
                MeasurementManager.Instance.StartMeasurement();

                drag.gameObject.SetActive(true);
            }
            else
            {
                MeasurementManager.Instance.ExitMeasurement();

                drag.gameObject.SetActive(false);
            }
        });

        xyzOpen.onValueChanged.AddListener((ison) => 
        {
            xyzOpen.GetComponentInChildren<Text>().text = ison ? "关闭XYZ轴" : "开启XYZ轴";

            MeasurementManager.Instance.isOpenXYZ = ison;

            if (!ison)
            {
                MeasurementManager.Instance.CloseLine_XYZ();
            }
        });
    }

    //public override void OnOpen(object data = null)
    //{
    //    //base.OnOpen(data);

    //    //sliderSpeed.maxValue = cameraCtr.maxMoveSpeed;

    //    //sliderSpeed.minValue = cameraCtr.minMoveSpeed;

    //    //sliderSpeed.value = Global.MoveSpeed;

    //    sliderScale.maxValue = MeasurementManager.Instance.maxScale;

    //    sliderScale.minValue = MeasurementManager.Instance.minScale;

    //    sliderSpeed.onValueChanged.AddListener(SetCameraMoveSpeed);

    //    sliderScale.onValueChanged.AddListener(SetPointScale);

    //    sliderScale.SetValueWithoutNotify(MeasurementManager.Instance.pointScaleCoe);

    //    //Global.Measurement = true;
    //}

    private void SetPointScale(float arg0)
    {
        drag.SetScale(arg0);

        MeasurementManager.Instance.SetMarkObjScale(arg0);
    }

    private void SetCameraMoveSpeed(float arg0)
    {
        //Global.MoveSpeed = sliderSpeed.value;
    }

    public void UpdateScroll()
    {
        drag.transform.position = originalPos;

        if (MeasurementManager.Instance.lines.Count <= 0)
        {
            return;
        }

        GameObject item = Instantiate(historyItem, content);

        Text itemText = item.GetComponentInChildren<Text>();

        itemText.text = (MeasurementManager.Instance.lines[MeasurementManager.Instance.lines.Count - 1].dis * 1000).ToString("0") + "mm";

        itemText.text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + itemText.text;

        item.gameObject.SetActive(true);

        content.sizeDelta = new Vector2(content.sizeDelta.x, MeasurementManager.Instance.lines.Count * 30);

        content.anchoredPosition = content.sizeDelta.y > 120.0f ? new Vector2(0, content.sizeDelta.y - 120.0f) : Vector2.zero;
    }

    public void ClearAllData()
    {
        //ClearParentContent(content);

        content.sizeDelta = new Vector2(content.sizeDelta.x, 0);

        MeasurementManager.Instance.ExitMeasurement();

        MeasurementManager.Instance.ClearAll();

        drag.transform.position = originalPos;
    }

    //public override void OnClose(object data = null)
    //{
    //    drag.transform.position = originalPos;

    //    MeasurementManager.Instance.ExitMeasurement();

    //    MeasurementManager.Instance.ClearAll();

    //    //base.OnClose(data);
    //}

    //临时替换
    private void OnEnable()
    {
        //sliderSpeed.maxValue = cameraCtr.maxMoveSpeed;

        //sliderSpeed.minValue = cameraCtr.minMoveSpeed;

        //sliderSpeed.value = Global.MoveSpeed;

        sliderScale.maxValue = MeasurementManager.Instance.maxScale;

        sliderScale.minValue = MeasurementManager.Instance.minScale;

        sliderSpeed.onValueChanged.AddListener(SetCameraMoveSpeed);

        sliderScale.onValueChanged.AddListener(SetPointScale);

        sliderScale.SetValueWithoutNotify(MeasurementManager.Instance.pointScaleCoe);

        //Global.Measurement = true;
    }

    private void OnDisable()
    {
        drag.transform.position = originalPos;

        MeasurementManager.Instance.ExitMeasurement();

        MeasurementManager.Instance.ClearAll();
    }
}

